package com.xwtech.cring.admin.controller;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.xwtech.cms.data.AbstractDataAdapter;
import com.xwtech.cms.data.db.JdbcTransaction;
import com.xwtech.cring.admin.pub.AdminUserRetConst;
import com.xwtech.framework.pub.result.ResultInfo;
import com.xwtech.framework.pub.result.ResultInfos;
import com.xwtech.framework.pub.web.RequestNameConstants;

/**
 * 后台管理员权限管理Controller
 * 
 * @author sunjs
 */
public class AdminUserPermissionsController extends MultiActionController {

	private static final Log log = LogFactory
			.getLog(AdminUserMenuController.class);

	/**
	 * update权限
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView updatePermissions(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		HashMap map = updatePermissionsFromXml(request);
		return new ModelAndView("/cring/jsp/admin/information.jsp",
				RequestNameConstants.INFORMATION, map);
	}
	
	/**
	 * <简述>更新用户组菜单权限
	 * <业务流程>
	 * 1.	删除原用户组和角色的关系
	 * 2.	查询菜单对应的角色id
	 * 		for (循环submenu_id) {
	 * 			2.1.	查询菜单对应的role_id
	 * 					for(循环role_id) {
	 * 						2.1.2.	建立新关系		
	 * 					}
	 * 		}
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	private HashMap updatePermissionsFromXml(HttpServletRequest request)
			throws SQLException {
		String adapter = request.getParameter("_adapter");
		String url = request.getParameter("_url");
		
		String deleteOldSysRoleRightSql = "/admin/AdminUserSysRoleRight_delete.xml";
		String queryRoleIdSql = "/admin/AdminUserRoleMenu_query.xml";
		String insertSysRoleRightSql = "/admin/AdminUserSysRoleRight_insert.xml";
		
		String[] submenuIds = request.getParameter("submenu_ids").split(",");
		logger.debug("submenuIds = " + submenuIds);
		logger.debug("user_role_id = " + request.getParameter("user_role_id"));
		
		
		AbstractDataAdapter ada = AbstractDataAdapter.getDataAdapter(request
				.getSession().getServletContext(), adapter);
		JdbcTransaction transaction = ada.getTransaction();
		int ret = -1;
		try {
			//1.	删除原用户组和角色的关系
			ret = transaction.update(deleteOldSysRoleRightSql, request);
			
			//2.	查询菜单对应的角色id
			List retRoleIdList = null;
			for (int i = 0; i < submenuIds.length; i++) {
				logger.debug("submenuIds[i] = " + submenuIds[i]);
				
				//2.1.	查询菜单对应的角色id
				request.setAttribute("submenu_id", submenuIds[i]);
				retRoleIdList = transaction.list(queryRoleIdSql, request);
				
				for (Iterator it = retRoleIdList.iterator(); it.hasNext();) {
					Map linMap = (Map)it.next();
					log.debug("role_id = " + linMap.get("ROLE_ID"));

					//2.1.2.	建立新关系
					request.setAttribute("role_id", linMap.get("ROLE_ID"));
					ret = transaction.update(insertSysRoleRightSql, request);
				}
			}
			transaction.commit();
		} catch (Exception e) {
			log.error("数据库操作时出错！", e);
			transaction.rollback();
			ret = -1;
		}

		ResultInfos resultInfos = new ResultInfos();
		if (ret < 0) {
			resultInfos.add(new ResultInfo(AdminUserRetConst.MENU_OP_FAILED));
			log.debug("数据库操作失败...");
		} else {
			resultInfos.add(new ResultInfo(AdminUserRetConst.MENU_OP_SUCCESS));
			log.debug("数据库操作成功...");
		}
		resultInfos.setGotoUrl(url);

		HashMap map = new HashMap();
		map.put(RequestNameConstants.RESULTINFOS, resultInfos);

		return map;
	}

}
